package org.swust.sysmonitorapp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.swust.sysmonitorapp.entity.DeviceStatus;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备状态 Mapper 接口
 *
 * @since 2024-03-21
 */
@Mapper
public interface DeviceStatusMapper extends BaseMapper<DeviceStatus> {

    /**
     * 更新设备状态
     *
     * @param deviceId 设备ID
     * @param status 状态
     * @param lastOnlineTime 最后在线时间
     * @return 影响的行数
     */
    @Update("UPDATE device_status SET status = #{status}, last_online_time = #{lastOnlineTime} WHERE device_id = #{deviceId}")
    int updateDeviceStatus(@Param("deviceId") String deviceId, @Param("status") String status, @Param("lastOnlineTime") LocalDateTime lastOnlineTime);

    /**
     * 查询所有设备状态
     *
     * @return 设备状态列表
     */
    @Select("SELECT device_id, status, last_online_time FROM device_status ORDER BY last_online_time DESC")
    List<DeviceStatus> getAllDeviceStatus();
}